package com.hkctp.mybatis.controller;

import com.github.pagehelper.PageHelper;
import com.hkctp.mybatis.domain.User;
import com.hkctp.mybatis.model.ResultMap;
import com.hkctp.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    private final ResultMap resultMap;

    @Autowired
    public UserController(ResultMap resultMap) {
        this.resultMap = resultMap;
    }

    @RequestMapping(value = "/getMessage", method = RequestMethod.GET)
    public ResultMap getMessage() {
        return resultMap.success().message("您拥有用户权限，可以获得该接口的信息！");
    }

    @Autowired
    private UserService userService;

    @RequestMapping("/likeName")
    public List<User> likeName(String name){
        PageHelper.startPage(3,2);      //进行分页操作
        return userService.likeName(name);
    }

    @RequestMapping("/save")
    public User save(){
        User user = new User();
        user.setName("喻老大");
        userService.save(user);
        return user;
    }

    @RequestMapping("/update")
    public User updateUser(){
        User user = new User();
        user.setId(17);
        user.setName("赵哥");
        userService.updateUser(user);
        return user;
    }

    @RequestMapping("/delete")
    public User deleteUser(){
        User user = new User();
        user.setId(21);
        userService.deleteUser(user);
        return user;
    }
}
